package com.example.blinkhome.activity;

import com.example.blinkhome.R;
import com.example.blinkhome.R.id;
import com.example.blinkhome.R.layout;
import com.example.blinkhome.R.menu;
import com.example.blinkhome.fragment.FindFragment;
import com.example.blinkhome.fragment.HomeFragment;
import com.example.blinkhome.fragment.MineFragment;
import com.example.blinkhome.fragment.ShopFragment;
import com.orhanobut.logger.Logger;

import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;


//https://git.oschina.net/food91/BlinkHome.git

public class MainActivity extends ActionBarActivity {
	
	private Button[] buttomBbutton;
	HomeFragment oneFragment;
	private LinearLayout l1,l2,l3,l4;
	private Fragment twoFragment,threeFragment,fourFragment;
	private FragmentManager mFragmentManager;
	private Handler mHandler=new Handler(){
		
		public void handleMessage(Message msg) {
			
			switch (msg.what) {
			case 0:
	
				break;

			default:
				break;
			}
			
		};
		
	};
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
		Logger.init();
        initViewData();
        findview();
        buttonClick();
        setTabSelection(0);
    }
    
    private void findview(){
      
    	l1=(LinearLayout) findViewById(R.id.ll_1);
    	l2=(LinearLayout) findViewById(R.id.ll_2);
    	l3=(LinearLayout) findViewById(R.id.ll_3);
    	l4=(LinearLayout) findViewById(R.id.ll_4);
    	
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
      
   
    }
    
	private void setTabSelection(int index) {
		int count;
		FragmentTransaction transaction = mFragmentManager
				.beginTransaction();
		switch (index) {
		case 0:
			transaction.replace(R.id.activity_device_framelayout,
					oneFragment);
			break;
		case 1:
			transaction.replace(R.id.activity_device_framelayout,
					twoFragment);
			break;
		case 2:

			transaction.replace(R.id.activity_device_framelayout,
					threeFragment);

			break;
		case 3:
			transaction.replace(R.id.activity_device_framelayout,
					fourFragment);
			break;
		default:
			break;
		}
		transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
		transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
		transaction.commit();
	}
    
    private OnClickListener itemClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.ll_1:
				  setTabSelection(0);
				break;
			case R.id.ll_2:
				  setTabSelection(1);
				break;
			case R.id.ll_3:
				  setTabSelection(2);
				break;
			case R.id.ll_4:
				  setTabSelection(3);
				break;
			default:
				break;
			}
		}
	};
    
    private void buttonClick(){
  
    	l1.setOnClickListener(itemClickListener);
    	l2.setOnClickListener(itemClickListener);
    	l3.setOnClickListener(itemClickListener);
    	l4.setOnClickListener(itemClickListener);
    	
    }
    
    private void initViewData(){
    	
    	buttomBbutton=new Button[4];
    	oneFragment=new HomeFragment();
    	twoFragment=new FindFragment();
    	threeFragment=new ShopFragment();
    	fourFragment=new MineFragment();
    	mFragmentManager = getFragmentManager();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
